﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Unis.Framework.Core.Utility;

using KingRoad.EPM.CompletionMaterialsEntity.Model;
using KingRoad.EPM.CompletionMaterialsDAL.BcdFileDataListService;
using KingRoad.EPM.CompletionMaterialsDAL.BcdFileListService;


namespace KingRoad.EPM.CompletionMaterialsBLL.UIRules
{
    /// <summary> 
    /// 归档类型表与案卷目录对应关系表UIRule 
    /// </summary> 
    public class BcdFiledatalistUIRule
    {
        private readonly IBcdFiledatalistService _bcdFiledatalistService;
        private BcdFilelistUIRule _bcdFilelistUIRule;
        

        public BcdFiledatalistUIRule(){}
        
        /// <summary> 
        /// construct function... 
        /// </summary> 
        public BcdFiledatalistUIRule(IBcdFiledatalistService  bcdFiledatalistService)
        {
           _bcdFiledatalistService=bcdFiledatalistService;
        }

        /// <summary>
        /// 根据条件获取信息
        /// </summary>
        /// <returns></returns>
        public List<BcdFiledatalistInfo> GetEntityListBySql(string sql)
        {
            var ret = _bcdFiledatalistService.GetEntityList(sql);
            return ret;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertEntity(BcdFiledatalistInfo entity)
        {
            var ret = _bcdFiledatalistService.InsertEntity(entity);
            return ret > 0;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteEntity(string id)
        {
            var ret = _bcdFiledatalistService.DeleteEntityByStringID(id);
            return ret > 0;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteEntity(BcdFiledatalistInfo info)
        {
            //要判断有没有 别的案卷类型 关联该案卷 没有才能 同时删除案卷
            List<BcdFiledatalistInfo> fdlList = _bcdFiledatalistService.GetEntityList(" Fl_ID='" + info.Fl_ID + "' ");
            if (fdlList.Count == 1)
            {
                _bcdFilelistUIRule = new BcdFilelistUIRule(new BcdFilelistService());
                //删除案卷
                _bcdFilelistUIRule.DeleteEntity(info.Fl_ID);
            }

            var ret = _bcdFiledatalistService.DeleteEntity(info);
            return ret > 0;
        }
    }
}

